// pages/setting/setting.js
var utils = require('../../utils/util.js');
const app = getApp();
var date = new Date();
let network = require("../../utils/network.js");
let url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:{},
    arrSex: ['请选择', '男', '女'],
    indexSex: 0,
    endDate: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(),
    date: "请选择日期"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
        that.list()
  },
  // 选择性别
  bindSexChange: function (e) {
    this.setData({
      indexSex: e.detail.value
    })
  },
  // 选择日期
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  list(){
    var that = this
    network.request({
      url: url + '/AppletsApi/Index/getUserInfo.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        const res = data.data.data
        if (res.sex == "男") {
          var indexSex = 1;
        }
        if (res.sex == "女") {
          var indexSex = 2;
        }
        if (res.sex == "") {
          var indexSex = 0;
        } 
        that.setData({
          list: res,
          date: res.birthday,
          indexSex: indexSex
        })
      }
    })
  },

  //提交
  bindSubmit: function (e) {
    console.log(e);
    var _this = this;
    network.request({
      url: url + '/AppletsApi/Index/editUserinfo.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        uname: e.detail.value.uname,
        sex: _this.data.indexSex,
        birthday: _this.data.date,
        weixin: e.detail.value.weixin,
        qq: e.detail.value.qq,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        const res = data.data
        console.log(res)
        if (res.code == 200){
          showMsg("成功修改")
        }else{
          showMsg("网络超时,请稍后再试")
        }
        
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function showMsg(str) {
  wx.showToast({
    title: str,
    icon: 'success',
    duration: 2000,
    mask: true
  });
}